#ifndef FILE_H
#define FILE_H
#pragma once

#include <string>
/*	TwFile
------------------------------------------------------------------------------------------
	
	Basic File IO routines
	
------------------------------------------------------------------------------------------
*/
using namespace std;
class TwFile
{
public:

	// data Types & Constants...
	enum FILE_ACCESS
	{
		READ_ONLY,
		WRITE_ONLY,
		READ_WRITE,
		APPEND_ONLY,
		READ_AND_APPEND
	};

	// Creators...
	TwFile();
	TwFile(const string& FileName, int AccessRights=READ_WRITE);
	~TwFile();

	// file creation
	bool open();
	bool open(const string& FileName, int AccessRights=READ_WRITE);
	bool create();
	bool create(const string& FileName);
	void close();

	// push/pull operators...
	TwFile& operator>>(char& data);
	TwFile& operator>>(unsigned char& data);
	TwFile& operator>>(short& data);
	TwFile& operator>>(unsigned short& data);
	TwFile& operator>>(unsigned int& data);
	TwFile& operator>>(int& data);
	TwFile& operator>>(float& data);
	TwFile& operator>>(bool& data);

	TwFile& operator<<(char data);
	TwFile& operator<<(unsigned char data);
	TwFile& operator<<(short data);
	TwFile& operator<<(unsigned short data);
	TwFile& operator<<(unsigned  data);
	TwFile& operator<<(int data);
	TwFile& operator<<(float data);
	TwFile& operator<<(bool data);

	// writing functions
	bool write(const void *Src, int BytesToWrite);
	bool write(const string& string, int* pBytesWritten=0);
	bool write(char Value);
	bool write(unsigned char Value);
	bool write(short Value);
	bool write(unsigned short Value);
	bool write(unsigned int Value);
	bool write(int Value);
	bool write(float Value);
	bool write(bool Value);

	// reading functions
	bool read(void *Dest, int BytesToRead);
	bool read(string& string);
	bool read(char& Value);
	bool read(unsigned char& Value);
	bool read(short& Value);
	bool read(unsigned short& Value);
	bool read(unsigned int& Value);
	bool read(int& Value);
	bool read(float& Value);
	bool read(bool& Value);

	// file position
	int position();
	bool setPosition(int NewPos);
	bool seek(long Offset);

	// accessors...
	bool isFileOpen();
	const string& name();
	int size();

private:

	// data...
	int m_fileAccess;
	void *m_fileHandle;
	string m_fileName;

	// Nonexistant Functions...
	TwFile( const TwFile&  Src);
	TwFile& operator=( const TwFile& Src);
	bool operator == ( const TwFile&  Src) const;
};

//- Inline Functions ---------------------------------------------------------------------

//. Creators .............................................................................
inline TwFile::TwFile()
:m_fileHandle(0)
{
	m_fileName.erase();
}

inline TwFile::TwFile(const string& FileName, int AccessRights)
:m_fileHandle(0)
,m_fileName(FileName)
,m_fileAccess(AccessRights)
{
}

inline TwFile::~TwFile()
{
	close();
}

//. Operators ............................................................................

inline TwFile& TwFile::operator>>(char& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(unsigned char& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(short& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(unsigned short& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(unsigned int& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(int& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(float& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator>>(bool& data)
{
	read((void*)&data, sizeof(data));
	return(*this);
}


inline TwFile& TwFile::operator<<(char data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(unsigned char data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(short data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(unsigned short data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(unsigned int data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(int data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(float data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}

inline TwFile& TwFile::operator<<(bool data)
{
	write((void*)&data, sizeof(data));
	return(*this);
}


//. Accessors ............................................................................

inline bool TwFile::isFileOpen()
{
	return (m_fileHandle ? true : false);
}

inline const string& TwFile::name()
{
	return (m_fileName);
}
 

#endif  
